﻿using System;

namespace Playground.Scratches
{
    /// <summary>
    /// Scratches of notes and examples
    /// </summary>
    public class Samples
    {
        /// <summary>
        /// Dynamic initialization where you can set the value with substitution
        /// while declaring the variable
        /// </summary>
        /// <example>
        /// <code>
        /// const int i = 10 % 3;
        /// Console.WriteLine("playing with MOD {0}", i);
        /// </code>
        /// <returns>1</returns>
        /// </example>
        public static void DynamicInitialization()
        {
            const int i = 10 % 3;
            Console.WriteLine("playing with MOD {0}", i);
        }
        /// <summary>
        /// Char is an unnassinged 16-bit type. Having a range os 0 to 65535
        /// </summary>
        /// <example> 
        /// <code>
        /// const char ch = 'x';
        /// Console.WriteLine("playing with Char {0}", ch);
        /// </code>
        /// <returns>x</returns>
        /// </example>
        public static void Chars()
        {
            const char ch = 'x';
            Console.WriteLine("playing with Char {0}", ch);
        }

        /// <summary>
        /// String Formatting examples
        /// </summary>
        /// <remarks>
        /// Google - "csharp string Formatting"
        /// </remarks>
        /// <example> 
        /// <code>
        /// Console.WriteLine("Here is 10/3 : {0:#.##}",10.0/3.0);
        /// </code>
        /// <returns>Here is 10/3 : 3.33</returns>
        /// </example>
        public static void StringFormatting()
        {
            Console.WriteLine("Here is 10/3 : {0:#.##}", 10.0 / 3.0);
        }

        /// <summary>
        /// Short-Circuit Logical Operators
        /// The short circuit operator will only evaluate the second operand when its neccessary
        /// </summary>
        /// <example> 
        /// <code>
        /// <![CDATA[
        ///    int d = 2;
        ///    int n = 10;
        ///    if (d != 0 && (n % d) == 0)
        ///    {
        ///        Console.WriteLine(d + " is a facotr of " + n);
        ///    }
        ///
        ///    d = 0;
        ///    if (d != 0 && (n % d) == 0)
        ///    {
        ///        Console.WriteLine(d + " is a facotr of " + n);
        ///    }
        /// 
        ///    if (d != 0 & (n % d) == 0)
        ///    {
        ///        Console.WriteLine(d + " is a facotr of " + n);
        ///    }
        /// ]]>
        /// </code>
        /// </example>
        public static void ShortCircuitLogicalOps()
        {
            int d = 2;
            const int n = 10;

            if (d != 0 && (n % d) == 0)
            {
                Console.WriteLine(d + " is a facotr of " + n);
            }

            d = 0; //now set the d to zero

            //since d is zero, the second operand is not evaluated
            if (d != 0 && (n % d) == 0)
            {
                Console.WriteLine(d + " is a facotr of " + n);
            }

            /*
             * now try same thing without shor-circuit operator
             * this will cause a divide-by-zero error
            */
            if (d != 0 & (n % d) == 0)
            {
                Console.WriteLine(d + " is a facotr of " + n);
            }
        }

        /// <summary>
        /// Switch provides for a multiway branch.
        /// </summary>
        /// <example> 
        /// <code>
        /// <![CDATA[
        ///     for(int i=0;i<=10;i++)
        ///    {
        ///        switch(i)
        ///        {
        ///            case 1 :
        ///            case 2 :
        ///            case 3 :
        ///                Console.WriteLine("i is 1,2, or 3");
        ///                break;
        ///            case 4 :
        ///            case 5 :
        ///            case 6 :
        ///            case 7 :
        ///            case 8 :
        ///            case 9 :
        ///            case 10 :
        ///                 Console.WriteLine("i is greater than 3");
        ///                 break;
        ///         }
        ///     }
        /// ]]>
        /// </code>
        /// </example>
        public static void SwitchStatements()
        {
            for (int i = 0; i <= 10; i++)
            {
                switch (i)
                {
                    case 1:
                    case 2:
                    case 3:
                        Console.WriteLine("i is 1,2, or 3");
                        break;
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                    case 10:
                        Console.WriteLine("i is greater than 3");
                        break;
                }
            }
        }

        /// <summary>
        /// Multi loop control variables
        /// </summary>
        /// <example> 
        /// <code>
        /// <![CDATA[
        ///     int i, j;
        ///     for(i=0, j=10; i< j; i++, j--)
        ///     {
        ///         Console.WriteLine("i and j: " + i + " " + j);
        ///     }
        /// ]]>
        /// </code>
        /// </example>
        public static void MultiLoopControl()
        {
            int i, j;
            for (i = 0, j = 10; i < j; i++, j--)
            {
                Console.WriteLine("i and j: " + i + " " + j);
            }
        }

        /// <summary>
        /// Demonstration of bubble sort
        /// using an array
        /// </summary>
        public static void BubbleSort()
        {
            int[] nums = { 99, -10, 100123, 18, -971, 5623, 463, -9, 287, 49 };

            int a;

            int size = 10; //number of elements to srot

            //display original array
            Console.Write("Original array is:");
            for (var i = 0; i < size; i++)
            {
                Console.Write(" " + nums[i]);
            }
            Console.WriteLine();

            //this is the bubble sort
            for (a = 1; a < size; a++)
            {
                for (var b = size - 1; b >= a; b--)
                {
                    //if out of order, exchange elements
                    if (nums[b - 1] > nums[b])
                    {
                        var t = nums[b - 1];
                        nums[b - 1] = nums[b];
                        nums[b] = t;
                    }
                }
            }

            //display the sorted array
            Console.Write("Sorted array is:");
            for (var i = 0; i < size; i++)
            {
                Console.Write(" " + nums[i]);
            }
            Console.WriteLine();
        }

        /// <summary>
        /// Multi Dimension Array Example
        /// </summary>
        public static void MultiDimensionalArrays()
        {
            int[,] table = { { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 } };
            for (int t = 0; t < 5; t++)
            {
                for (int i = 0; i < 2; i++)
                {
                    table[t, i] = (t * 4) + i + 1;
                    Console.Write(table[t, i] + " ");
                }
                Console.WriteLine();
            }
        }

        /// <summary>
        /// the ternary operator sample
        /// using the ?, it works as if-else statment
        /// </summary>
        public static void TernaryOperator()
        {
            int result;

            for (int i = -5; i < 6; i++)
            {
                // if i is not equal to zero
                // result = 100 / i
                // if it is, result = 0
                // when condition is true : when condition is false
                result = i != 0 ? 100 / i : 0;
                if (i != 0) 
                {
                    Console.WriteLine("100 / " + i + " is " + result);
                }
            }
            Console.WriteLine();
            for (int i = -5; i < 6; i++) 
            {
                //notice how we can avoid division by zero by using the Ternary operator
                if (i != 0 ? true : false) 
                {
                    Console.WriteLine("100 / " + i + " is " + 100 / i);
                }
            }
        }
    }
}
